package com.gz.purchase.library.pojo;


import com.gz.purchase.library.util.Constants;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * author wanner1991
 * 首页 订单实体类
 */
public class ProductInfo implements Serializable{
    private boolean isSelected ; // 用户是否选中
    private int selectNum ;// 用户选中的个数
    private int product_id ;//商品id
    private String product_name;//商品名称
    private double product_price; //商品单价
    private int product_num; //商品剩余量
    private int shop_id; //供应商id
    private String shop_name;//供应商名称
    private double supply_price;//供货价

    public double getSupply_price() {
        return supply_price;
    }

    public void setSupply_price(double supply_price) {
        this.supply_price = supply_price;
    }

    private String pic_url;// 图片地址，格式为mall/product/db5798a7b1a49ba659f793326fe5eb56.jpg

    public boolean isSelected() {
        return isSelected;
    }

    public void setIsSelected(boolean isSelected) {
        this.isSelected = isSelected;
    }

    public int getSelectNum() {
        return selectNum;
    }

    public void setSelectNum(int selectNum) {
        this.selectNum = selectNum;
    }

    public int getProduct_id() {
        return product_id;
    }

    public void setProduct_id(int product_id) {
        this.product_id = product_id;
    }

    public String getProduct_name() {
        return product_name;
    }

    public void setProduct_name(String product_name) {
        this.product_name = product_name;
    }

    public double getProduct_price() {
        return product_price;
    }

    public void setProduct_price(double product_price) {
        this.product_price = product_price;
    }

    public int getProduct_num() {
        return product_num;
    }

    public void setProduct_num(int product_num) {
        this.product_num = product_num;
    }

    public int getShop_id() {
        return shop_id;
    }

    public void setShop_id(int shop_id) {
        this.shop_id = shop_id;
    }

    public String getShop_name() {
        return shop_name;
    }

    public void setShop_name(String shop_name) {
        this.shop_name = shop_name;
    }

    public String getPic_url() {
        return pic_url;
    }

    public void setPic_url(String pic_url) {
        this.pic_url = pic_url;
    }

    public static List<ProductInfo> parse(JSONArray array) throws JSONException {
        if (array.length() <= 0 || array == null){
            return null;
        }
        ArrayList<ProductInfo> list = new ArrayList<ProductInfo>();
        ProductInfo info ;
        for (int i = 0 ; i < array.length() ; i++){
            info = new ProductInfo();
            JSONObject object = array.getJSONObject(i);
            info.product_id = object.getInt("product_id");
            info.product_name = object.getString("product_name");
            info.product_price = object.getDouble("product_price");
            info.supply_price = object.getDouble("supply_price");
            info.product_num = object.getInt("product_num");
            info.shop_id = object.getInt("shop_id");
            info.shop_name = object.getString("shop_name");
            info.pic_url = Constants.URL_PICTURE_HOST+object.getString("pic_url"); // 格式为: mall/product/db5798a7b1a49ba659f793326fe5eb56.jpg
            info.selectNum = 1;
            list.add(info);
        }
        return list;
    }

    /**
     * 代购订单页面 的解析方法，没有图片 的url
     * @param jsonArray
     * @return
     */
    public static List<ProductInfo> parse2(JSONArray jsonArray) throws JSONException {
        if (jsonArray.length() <= 0 || jsonArray == null){
            return null;
        }
        ArrayList<ProductInfo> list = new ArrayList<ProductInfo>();
        ProductInfo info = null;
        for (int i = 0 ; i < jsonArray.length() ; i++){
            info = new ProductInfo();
            JSONObject object = jsonArray.getJSONObject(i);
            info.product_id = object.getInt("product_id");
            info.product_name = object.getString("product_name");
            info.product_price = object.getDouble("product_price");
            info.product_num = object.getInt("product_num");
            info.shop_id = object.getInt("shop_id");
            info.shop_name = object.getString("shop_name");
            info.supply_price = object.getDouble("supply_price");
            list.add(info);
        }
        return list;
    }
}
